home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / LOCALM~1 / Filter.bas < prev    next >
BASIC Source File  |  1997-06-14  |  3KB  |  119 lines

  1. Attribute VB_Name = "MFilter"
  2. Option Explicit
  3.  
  4. Public Enum EErrorFilter
  5.     eeBaseFilter = 13490    ' Filter
  6. End Enum
  7.  
  8. Sub FilterTextFile(filter As IFilter)
  9.     
  10.     BugAssert filter.Source <> sEmpty
  11.     ' Target can be another file or replacement of current file
  12.     Dim sTarget As String, fReplace As Boolean
  13.     sTarget = filter.Target
  14.     If sTarget = sEmpty Or sTarget = filter.Source Then
  15.         sTarget = MUtility.GetTempFile("FLT", ".")
  16.         fReplace = True
  17.     End If
  18.     
  19.     ' Open input file
  20.     On Error GoTo FilterTextError1
  21.     Dim nIn As Integer, nOut As Integer
  22.     nIn = FreeFile
  23.     Open filter.Source For Input Access Read Lock Write As #nIn
  24.  
  25.     ' Open target output file
  26.     On Error GoTo FilterTextError2
  27.     nOut = FreeFile
  28.     Open sTarget For Output Access Write Lock Read Write As #nOut
  29.  
  30.     ' Filter each line
  31.     On Error GoTo FilterTextError3
  32.     Dim sLine As String, iLine As Long, eca As EChunkAction
  33.     Do Until EOF(nIn)
  34.         Line Input #nIn, sLine
  35.         iLine = iLine + 1
  36.         eca = filter.Translate(sLine, iLine)
  37.         Select Case eca
  38.         Case ecaAbort
  39.             GoTo FilterTextError3   ' Stop processing
  40.         Case ecaTranslate
  41.             Print #nOut, sLine      ' Write modified line to output
  42.         Case ecaSkip
  43.                                     ' Ignore
  44.         Case Else
  45.             BugAssert True          ' Should never happen
  46.         End Select
  47.     Loop
  48.     
  49.     ' Close files
  50.     On Error GoTo FilterTextError1
  51.     Close nIn
  52.     Close nOut
  53.     If fReplace Then
  54.         ' Destroy old file and replace it with new one
  55.         Kill filter.Source
  56.         On Error Resume Next   ' No more errors allowed
  57.         Name sTarget As filter.Source
  58.         ' If this fails, you're in trouble
  59.         BugAssert Err = 0
  60.     End If
  61.     Exit Sub
  62.         
  63. FilterTextError3:
  64.     Close nOut
  65. FilterTextError2:
  66.     Close nIn
  67. FilterTextError1:
  68.     MErrors.ErrRaise Err
  69. End Sub
  70. '
  71.  
  72.  
  73. ' Applies filter to the IFilter.Source string and
  74. ' and saves the result in the IFilter.Target string.
  75. Sub FilterText(filter As IFilter)
  76.     Dim sSrc As String, sDst As String
  77.     Dim iLine As Integer, sLine As String
  78.     
  79.     sSrc = filter.Source
  80.     sLine = MUtility.GetNextLine(sSrc)
  81.     Do While sLine <> sEmpty
  82.         ' Strip off sCrLf
  83.         sLine = MUtility.RTrimLine(sLine)
  84.         iLine = iLine + 1
  85.         Select Case filter.Translate(sLine, iLine)
  86.         Case ecaAbort:
  87.             Exit Sub
  88.         Case ecaTranslate:
  89.             sDst = sDst & sLine & sCrLf
  90.         Case ecaSkip:
  91.         Case Else:
  92.             BugAssert True
  93.         End Select
  94.         sLine = MUtility.GetNextLine
  95.     Loop
  96.     filter.Target = sDst
  97. End Sub
  98.  
  99. #If fComponent = 0 Then
  100. Private Sub ErrRaise(e As Long)
  101.     Dim sText As String, sSource As String
  102.     If e > 1000 Then
  103.         sSource = App.ExeName & ".Filter"
  104.         Select Case e
  105.         Case eeBaseFilter
  106.             BugAssert True
  107.        ' Case ee...
  108.        '     Add additional errors
  109.         End Select
  110.         Err.Raise COMError(e), sSource, sText
  111.     Else
  112.         ' Raise standard Visual Basic error
  113.         sSource = App.ExeName & ".VBError"
  114.         Err.Raise e, sSource
  115.     End If
  116. End Sub
  117. #End If
  118.  
  119.